package com.xxx.controller;


import com.xxx.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/file-load")
@Slf4j
public class LoadController {

    @Value("${file.path}")
    private String filePath;

    @Value("${image.path}")
    private String imagePath;

    @PostMapping("/upload")
    public Result loadFile(MultipartFile file) throws IOException {
        String path = null;

        String[] arr = {"jpeg","png","jpg"};
        System.out.println(file);
        String fileName = file.getOriginalFilename();
        int indexOf = fileName.lastIndexOf('.');
        //文件格式
        String suffix = fileName.substring(indexOf);
        for (String s : arr) {
            if (suffix.contains(s)){
                path = imagePath;
                break;
            }
            path = filePath;
        }

        String uuid = UUID.randomUUID().toString();
        fileName = uuid + suffix;

        log.info("file:{}",fileName);
        //判断文件夹是否存在
        File current = new File(path);
        if (!current.exists()){
            current.mkdirs();
        }

        file.transferTo(new File(path + fileName));

        return Result.success("上传成功",fileName);
    }

    @GetMapping("/image-download")
    public void download(String name, HttpServletResponse resp) {

        resp.setContentType("image/jpeg");
        if (name == "" || name == null) return;
        try (
                FileInputStream fos = new FileInputStream(imagePath + name);
                ServletOutputStream outputStream = resp.getOutputStream()
        ) {
            int len = 0;
            byte[] bytes = new byte[1024];
            while ((len = fos.read(bytes)) != -1) {
                outputStream.write(bytes, 0, len);
                outputStream.flush();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    @PostMapping("/attachments")
    public Result loadAttachments(MultipartFile file) throws IOException {
        String path = filePath;

        System.out.println(file);
        String fileName = file.getOriginalFilename();
        int indexOf = fileName.lastIndexOf('.');
        //文件格式
        String suffix = fileName.substring(indexOf);



        String uuid = UUID.randomUUID().toString();
        fileName = uuid + suffix;

        log.info("file:{}",fileName);
        //判断文件夹是否存在
        File current = new File(path);
        if (!current.exists()){
            current.mkdirs();
        }

        file.transferTo(new File(path + fileName));

        return Result.success("上传成功",fileName);
    }

}
